package com.cloudft.http.services;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import android.content.Context;
import android.util.Log;

/**
 * StorageService - This class is used by the CacheService to store/read the
 * cached objects 
 */
public class StorageService {

	/**
	 * serializeObject() - This method serializes the object into a file
	 * @param fileName - Name of the file where we want to write the object
	 * @param Context - Context object passed by the calling service/activity 
	 */
	public void serializeObject(String fileName, Object obj,Context context){
		try {
			// Serialize to a file
			ObjectOutput out = new ObjectOutputStream(context.openFileOutput(fileName,0));
			out.writeObject(obj);
			out.close();

		} catch (IOException e) {
			Log.i("CacheService IOException :", e.getMessage());
		}
	}


	/**
	 * deserializeObject() - This method deserializes the object from a file
	 * @param fileName - Name of the file from where we want to read the object
	 * @param Context - Context object passed by the calling service/activity 
	 */
	public Object deserializeObject(String fileName,Context context){
		
		Object obj = null;
		try {
			// Deserialize from a file
			ObjectInputStream in = new ObjectInputStream(context.openFileInput(fileName));
			obj = in.readObject();
			in.close();

		} catch (ClassNotFoundException e) {
			Log.i("StorageService ClassNotFoundException :", e.getMessage());
		} catch (IOException e) {
			Log.i("StorageService IOException :", e.getMessage());
		}
		
		return obj;
	}

}
